package cn.wolfcode.mycat.util;

import java.sql.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Jason
 * @Date: 2021/03/15/10:38
 * @Description:
 */
public class Test {
	
	public static void Query() {
		Connection connection = null;
		try {
			String SDdriver = "com.mysql.cj.jdbc.Driver";
			String SDurl = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false";
			//emsdb替换成自己的数据库名称
			String SDuser = "root";
			String SDpassword = "1qaz@WSX";
			Class.forName(SDdriver);
			connection = DriverManager.getConnection(SDurl, SDuser, SDpassword);
		} catch (Exception e) {
			e.printStackTrace();
		}
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		String ss = "SELECT * from test";
		try {
			preparedStatement = connection.prepareStatement(ss);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				System.out.println(resultSet.getString("amount"));
			}
		} catch (SQLException ex) {
		} finally {
			closeAll(connection, preparedStatement,resultSet);
		}
	}
	
	public static void closeAll(Connection connection, PreparedStatement preparedStatement,ResultSet resultSet) {
		try {
			//按顺序关闭
			if(resultSet!=null) {
				resultSet.close();
			}
			if (preparedStatement!=null) {
				preparedStatement.close();
			}
			if (connection != null) {
				connection.close();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Query();
	}
}
